﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace 数据词典转Sql工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num = Convert.ToInt32(textBox5.Text);

            for (int n = 0; n < num; n++)
            {
                string nr = textBox3.Text.Trim();
                string[] line = nr.Split('\n');
                string primary = "";
                string sql = "CREATE TABLE `" + textBox2.Text.Trim() + textBox1.Text.Trim() + "` (\n";

                if (num > 1)
                {
                    sql= "CREATE TABLE `" + textBox2.Text.Trim() + textBox1.Text.Trim() + "_"+n.ToString()+"` (\n";
                }
                string zd = "";
                for (int i = 0; i < line.Length; i++)
                {
                    string lin = line[i].Trim();
                    string[] item = lin.Split('\t');
                    string ziduan = "";
                    if (item[2] == "ID")
                    {
                        ziduan = "`" + item[0] + "` " + item[1] + " unsigned NOT NULL AUTO_INCREMENT COMMENT '" + item[2] + "',";
                        primary = "PRIMARY KEY(`" + item[0] + "`)";
                    }
                    else
                    {
                        if (item.Length < 4 || item[3].Length < 1)
                        {
                            if (item.Length < 5 || item[4].Length < 1)
                            {
                                ziduan = "`" + item[0] + "` " + item[1] + " NOT NULL COMMENT '" + item[2] + "',";
                            }
                            else
                            {
                                if (item[1].Contains("int") || item[1].Contains("decimal"))
                                {
                                    ziduan = "`" + item[0] + "` " + item[1] + " NOT NULL DEFAULT " + item[4] + " COMMENT '" + item[2] + "',";
                                }
                                else
                                {
                                    ziduan = "`" + item[0] + "` " + item[1] + " NOT NULL DEFAULT '" + item[4] + "' COMMENT '" + item[2] + "',";
                                }
                            }
                        }
                        else
                        {
                            if (item.Length < 5 || item[4].Length < 1)
                            {
                                ziduan = "`" + item[0] + "` " + item[1] + " COMMENT '" + item[2] + "',";
                            }
                            else
                            {
                                if (item[1].Contains("int") || item[1].Contains("decimal"))
                                {
                                    ziduan = "`" + item[0] + "` " + item[1] + " DEFAULT " + item[4] + " COMMENT '" + item[2] + "',";
                                }
                                else
                                {
                                    ziduan = "`" + item[0] + "` " + item[1] + " DEFAULT '" + item[4] + "' COMMENT '" + item[2] + "',";
                                }
                            }
                        }
                    }
                    zd += ziduan + "\n";
                }
                zd += primary + "\n";
                if (primary.Length < 1)
                {
                    zd = zd.Trim().Substring(0, zd.Trim().Length - 1);
                }
                sql += zd + ") ENGINE=" + comboBox1.SelectedItem.ToString() + " DEFAULT CHARSET = utf8mb4;";
                RunSql(sql);
                sql = "";
            }
            //textBox4.Text = "";
            textBox3.Text = "";
            textBox1.Text = "";
            MessageBox.Show("成功");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RunSql(textBox4.Text);
            textBox4.Text = "";
                textBox3.Text = "";
                textBox1.Text = "";
            MessageBox.Show("成功");
        }

        private void RunSql(string sql)
        {
            try
            {
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = "Database="+textBox7.Text+";Data Source="+textBox4.Text+";User Id=root;Password="+textBox6.Text+"";
                conn.Open();
                sql = sql.Trim().Replace("\n", "").Replace("\r", "");
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                int i = Convert.ToInt32(cmd.ExecuteNonQuery());
                conn.Close();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
